/**
 * Returns the capabilities of the defined service.
 *
 * @param {string} service_type The type of the service, eg. "WMS" or "WFS".
 * @param {string} service_url The url of the service we're connecting to.
 * @param {string} service_version The version number of the service.
 * @param {string} service_format The OpenLayers format the service needs to parse the response.
 * @returns The capabilities of the service.
 */
function getCapabilities(service_type, service_url, service_version, service_format) {
	var data = $.ajax({
		type: "GET",
		url: service_url,
		data: {
			service: service_type,
			request: "GetCapabilities",
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			dataType: "xml",
			version: service_version
		},
		async: false
	}).responseText;
	
	var format = service_format;
	var capabilities = format.read(data);
	return capabilities;
}

/**
 * Returns the meta info of the WMS raster layers in the land survey service.
 * Notice! The request is NOT asynchronous.
 *
 * @param {string} wms_url The url for the land survey WMS interface.
 * @returns An OpenLayers capabilities object.
 */
function getWmsCapabilities(wms_url) {
	return getCapabilities("WMS", wms_url, "1.1.1", new OpenLayers.Format.WMSCapabilities());
}

/**
 * Returns the meta info of the WFS features in the land survey service.
 * Notice! The request is NOT asynchronous.
 *
 * @param {string} wfs_url The url for the land survey WFS interface.
 * @returns An OpenLayers capabilities object.
 */
function getWfsCapabilities(wfs_url) {
	return getCapabilities("WFS", wfs_url, "1.1.0", new OpenLayers.Format.WFSCapabilities());
}

/**
 * Returns the WMS layers.
 *
 * @param {object} capabilities An OpenLayers capabilities object.
 * @returns An array of layer objects.
 */
function getWmsLayers(capabilities) {
	var layers = [];
	$.each(capabilities.capability.layers, function(i, val) {
		var layer = {};
		layer.name = val.name; // Layer's name
		layer.title = val.title; // Layer's title to be used in e.g. layer switcher
		layer.baselayer = val.opaque; // True or False depending on its opaque value
		layer.bounds = val.bbox['EPSG:3067'].bbox; // Array of bound values
		layer.minscale = val.minScale;
		layer.maxscale = val.maxScale;
		layer.res = val.bbox['EPSG:3067'].res // Object with keys 'x' and 'y'
		layers.push(layer);
	});
	return layers;
}